import numpy as np
import pandas as pd
from sklearn.preprocessing import OneHotEncoder
from sklearn.preprocessing import minmax_scale

data = pd.read_excel("last.xlsx")
data_encode = pd.DataFrame()

'''独热编码'''

# 被保人性别
onehot_encoder_1 = OneHotEncoder().fit(np.array(data["被保人性别"]).reshape(-1, 1))
for i in range(len(onehot_encoder_1.categories_[0])):
    data_encode.insert(loc=len(data_encode.columns),
                       value=pd.DataFrame(onehot_encoder_1.transform(np.array(data["被保人性别"]).reshape(-1, 1))
                                          .toarray())[i].tolist(),
                       column=onehot_encoder_1.categories_[0][i],
                       )

# 投保年龄段
onehot_encoder_1 = OneHotEncoder().fit(np.array(data["投保年龄段"]).reshape(-1, 1))
for i in range(len(onehot_encoder_1.categories_[0])):
    data_encode.insert(loc=len(data_encode.columns),
                       value=pd.DataFrame(onehot_encoder_1.transform(np.array(data["投保年龄段"]).reshape(-1, 1))
                                          .toarray())[i].tolist(),
                       column=onehot_encoder_1.categories_[0][i],
                       )

# 基本保额段
onehot_encoder_1 = OneHotEncoder().fit(np.array(data["基本保额段"]).reshape(-1, 1))
for i in range(len(onehot_encoder_1.categories_[0])):
    data_encode.insert(loc=len(data_encode.columns),
                       value=pd.DataFrame(onehot_encoder_1.transform(np.array(data["基本保额段"]).reshape(-1, 1))
                                          .toarray())[i].tolist(),
                       column=onehot_encoder_1.categories_[0][i],
                       )

# 城市线
onehot_encoder_1 = OneHotEncoder().fit(np.array(data["城市线"]).reshape(-1, 1))
for i in range(len(onehot_encoder_1.categories_[0])):
    data_encode.insert(loc=len(data_encode.columns),
                       value=pd.DataFrame(onehot_encoder_1.transform(np.array(data["城市线"]).reshape(-1, 1))
                                          .toarray())[i].tolist(),
                       column=onehot_encoder_1.categories_[0][i],
                       )

'''归一化'''

data_encode.insert(loc=len(data_encode.columns), value=minmax_scale(data["缴费年限"].tolist()), column="缴费年限")
data_encode.insert(loc=len(data_encode.columns), value=minmax_scale(data["保单件数"].tolist()), column="保单件数")
data_encode.insert(loc=len(data_encode.columns), value=minmax_scale(data["各省人口数"].tolist()), column="各省人口数")
data_encode.insert(loc=len(data_encode.columns), value=minmax_scale(data["人均生产总值20152020方差"].tolist()),
                   column="人均生产总值20152020方差")
data_encode.insert(loc=len(data_encode.columns), value=minmax_scale(data["人均蔬菜"].tolist()), column="人均蔬菜")
data_encode.insert(loc=len(data_encode.columns), value=minmax_scale(data["人均肉类"].tolist()), column="人均肉类")
data_encode.insert(loc=len(data_encode.columns), value=minmax_scale(data["蛋类"].tolist()), column="蛋类")
data_encode.insert(loc=len(data_encode.columns), value=minmax_scale(data["奶类"].tolist()), column="奶类")
data_encode.insert(loc=len(data_encode.columns), value=minmax_scale(data["糖"].tolist()), column="糖")
data_encode.insert(loc=len(data_encode.columns), value=minmax_scale(data["均值人均水资源"].tolist()), column="均值人均水资源")
data_encode.insert(loc=len(data_encode.columns), value=minmax_scale(data["方差人均水资源"].tolist()), column="方差人均水资源")
data_encode.insert(loc=len(data_encode.columns), value=minmax_scale(data["财政支出医疗卫生1520方差"].tolist()),
                   column="财政支出医疗卫生1520方差")
data_encode.insert(loc=len(data_encode.columns), value=minmax_scale(data["卫生机构数均值"].tolist()), column="卫生机构数均值")
data_encode.insert(loc=len(data_encode.columns), value=minmax_scale(data["居民年住院率"].tolist()), column="居民年住院率")
data_encode.insert(loc=len(data_encode.columns), value=minmax_scale(data["最高气温均值"].tolist()), column="最高气温均值")
data_encode.insert(loc=len(data_encode.columns), value=minmax_scale(data["最高气温方差"].tolist()), column="最高气温方差")
data_encode.insert(loc=len(data_encode.columns), value=minmax_scale(data["日照时数"].tolist()), column="日照时数")
data_encode.insert(loc=len(data_encode.columns), value=minmax_scale(data["pm2_5"].tolist()), column="pm2_5")
data_encode.insert(loc=len(data_encode.columns), value=minmax_scale(data["o3"].tolist()), column="o3")

data_encode.insert(loc=len(data_encode.columns), value=data["是否理赔"].tolist(), column="是否理赔")

data_encode.to_csv("last_encode.csv", header=True, index=False)
